local huilan = fk.CreateSkill{
    name = "ym2__huilan",
}
Fk:loadTranslationTable{
    ["ym2__huilan"] = "回澜",
    [":ym2__huilan"] = "当你手牌中点数最大的牌成为<font color='#CC3131'>♥</font>牌时，可展示之，若之本回合开始时为你手牌中点数第X大的牌，摸X张牌。",
    ["@ym2__huilan-turn-inhand"] = "回澜",
    ["@[ym2__huilan]"] = "回澜",
    ["#ym2__huilan-show"] = "回澜：是否展示（%arg），然后摸 %arg2 张牌",
}
Fk:addQmlMark{
    name = "ym2__huilan",
    qml_path = "",
    how_to_show = function (name, value, player)
        if player:hasSkill(huilan.name) and type(value) == "table" and player:getHandcardNum() > 0 then
            local number = 0
            for _, cardID in ipairs(player:getCardIds("h")) do
                local card = Fk:getCardById(cardID)
                if card.number > number then
                    number = card.number
                end
            end
            local cardNumbers = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}
            return table.concat(table.map(value,Util.TranslateMapper)).." "..cardNumbers[number]
        end
        return " "
    end,
}
huilan:addEffect(fk.AfterCardsMove,{
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(huilan.name) and data.extra_data and data.extra_data.ym2__huilan
    end,
    on_cost = function (self, event, target, player, data)
        local cards = {}
        local x = 0
        local hs = player:getCardIds("h")
        for _, cardID in ipairs(hs) do
            local card = Fk:getCardById(cardID)
            if card.suit == Card.Heart and not table.find(hs,function (id)
                return Fk:getCardById(id).number > card.number
            end) then
                table.insertIfNeed(cards,cardID)
                if card:getMark("@ym2__huilan-turn-inhand") > 0 then
                    x = x + card:getMark("@ym2__huilan-turn-inhand")
                end
            end
        end
        if #cards > 0 then
            local str = table.concat(table.map(cards,function (cardID)
                return Fk:getCardById(cardID):toLogString()
            end),"、")
            if player.room:askToSkillInvoke(player,{
                skill_name = huilan.name,
                prompt = "#ym2__huilan-show:::"..str..":"..x
            }) then
                event:setCostData(self,{cards = cards,num = x})
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local cards = event:getCostData(self).cards
        local num = event:getCostData(self).num
        player:showCards(cards)
        if num > 0 then
            player:drawCards(num,huilan.name)
        end
    end,
})
huilan:addEffect(fk.AfterCardsMove,{
    can_refresh = function (self, event, target, player, data)
        if player:hasSkill(huilan.name) then
            local mark = player:getTableMark("@[ym2__huilan]")
            local new_mark = {}
            local cards = player:getCardIds("h")
            for _, cardID in ipairs(cards) do
                local card = Fk:getCardById(cardID)
                if not table.find(cards,function (id)
                    return Fk:getCardById(id).number > card.number
                end) then
                    table.insertIfNeed(new_mark,card:getSuitString(true))
                end
            end
            player.room:setPlayerMark(player,"@[ym2__huilan]",new_mark)
            if table.contains(new_mark,"log_heart") and not table.contains(mark,"log_heart") then
                return true
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        p("true")
        data.extra_data = data.extra_data or {}
        data.extra_data.ym2__huilan = true
    end,
})
huilan:addAcquireEffect(function (self, player, is_start)
    local new_mark = {}
    local cards = player:getCardIds("h")
    for _, cardID in ipairs(cards) do
        local card = Fk:getCardById(cardID)
        if not table.find(cards,function (id)
            return Fk:getCardById(id).number > card.number
        end) then
            table.insertIfNeed(new_mark,card:getSuitString(true))
        end
    end
    player.room:setPlayerMark(player,"@[ym2__huilan]",new_mark)
end)
huilan:addEffect(fk.TurnStart,{
    can_refresh = function (self, event, target, player, data)
        return player:hasSkill(huilan.name) and table.filter(player:getCardIds("h"),function (cardID)
            return Fk:getCardById(cardID).suit == Card.Heart
        end)
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local numbers = {}
        local cards = player:getCardIds("h")
        for _, cardID in ipairs(cards) do
            local card = Fk:getCardById(cardID)
            table.insert(numbers,card.number)
        end
        table.sort(numbers,function (a, b) return a > b end)
        for _, cardID in ipairs(cards) do
            local card = Fk:getCardById(cardID)
            if card.suit == Card.Heart then
                local index = table.indexOf(numbers,card.number)
                if index ~= -1 then room:setCardMark(card,"@ym2__huilan-turn-inhand",index) end
            end
        end
    end,
})
return huilan